package one.bugu.android.demon.util;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.widget.Toast;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.common.util.PermissionManager;
import com.juefeng.android.framework.common.util.ToastUtils;
import com.juefeng.sharelib.ShareService;
import com.umeng.socialize.bean.SHARE_MEDIA;


import java.io.File;

import one.bugu.android.demon.ui.dialog.DialogShare;

/**
 * 分享
 */
public class ShareDialogUtils {
    /**
     * 单例对象
     */
    private static volatile ShareDialogUtils singleton = null;

    private ShareDialogUtils() {

    }

    /**
     * 获取单例对象
     *
     * @return 单例对象
     */
    public static ShareDialogUtils getInstance() {
        if (singleton == null) {
            synchronized (ShareDialogUtils.class) {
                if (singleton == null) {
                    singleton = new ShareDialogUtils();
                }
            }
        }
        return singleton;
    }

    /**
     * 图片分享
     *
     * @param activity
     * @param fileName
     */
    public void shareImage(final Activity activity, final String fileName) {
        DialogShare dialogShare = new DialogShare(activity);
        dialogShare.setOnDialogVersionListener(new DialogShare.OnDialogVersionListener() {
            @Override
            public void onSure(String string) {
                if (string != null && string.equals("1")) {
                    ShareService shareService = new ShareService();
                    shareService.shareImage(activity, new File(fileName), new ShareService.ShareListener() {
                        @Override
                        public void shareSuccess(SHARE_MEDIA share_media1) {

                        }

                        @Override
                        public void shareCancel() {
                            ToastUtils.custom("已取消分享");
                        }
                    }, SHARE_MEDIA.WEIXIN);
                } else if (string != null && string.equals("2")) {
                    ShareService shareService = new ShareService();
                    shareService.shareImage(activity, new File(fileName), new ShareService.ShareListener() {
                        @Override
                        public void shareSuccess(SHARE_MEDIA share_media1) {

                        }

                        @Override
                        public void shareCancel() {
                            ToastUtils.custom("已取消分享");
                        }
                    }, SHARE_MEDIA.WEIXIN_CIRCLE);
                } else if (string != null && string.equals("3")) {
                    ImageUtils.notifyCarme(activity, fileName);
                    ToastUtils.custom("保存成功");
                }
            }
        });
        dialogShare.show();
    }
    /**
     * 图片分享 bitmap
     *
     * @param activity
     * @param bmpFile
     */
    public void shareBmpImage(final Activity activity, final Bitmap bmpFile) {
        DialogShare dialogShare = new DialogShare(activity,1, false);
        dialogShare.setOnDialogVersionListener(new DialogShare.OnDialogVersionListener() {
            @Override
            public void onSure(String string) {
                if (string != null && string.equals("1")) {
                    ShareService shareService = new ShareService();
                    shareService.shareBmpImage(activity, bmpFile, new ShareService.ShareListener() {
                        @Override
                        public void shareSuccess(SHARE_MEDIA share_media1) {

                        }

                        @Override
                        public void shareCancel() {
                            ToastUtils.custom("已取消分享");
                        }
                    }, SHARE_MEDIA.WEIXIN);
                } else if (string != null && string.equals("2")) {
                    ShareService shareService = new ShareService();
                    shareService.shareBmpImage(activity, bmpFile, new ShareService.ShareListener() {
                        @Override
                        public void shareSuccess(SHARE_MEDIA share_media1) {

                        }

                        @Override
                        public void shareCancel() {
                            ToastUtils.custom("已取消分享");
                        }
                    }, SHARE_MEDIA.WEIXIN_CIRCLE);
                } else if (string != null && string.equals("3")) {
//                    ImageUtils.notifyCarme(activity, fileName);
//                    ToastUtils.custom("保存成功");
                }
            }
        });
        dialogShare.show();
    }

    /**
     * 链接分享
     *
     * @param activity
     */
    public void shareUrl(final Activity activity, final String shareUrl) {
        shareUrl(activity, shareUrl, "", "", "", false);
    }

    /**
     * 链接分享
     *
     * @param activity
     * @param shareUrl     分享链接地址
     * @param shareTitle
     * @param shareContent
     * @param imgUrl
     */
    public void shareUrl(final Activity activity, final String shareUrl, final String shareTitle,
                         final String shareContent, final Object imgUrl, boolean showTitle) {
        DialogShare dialogShare = new DialogShare(activity, 1, showTitle);
        dialogShare.setOnDialogVersionListener(new DialogShare.OnDialogVersionListener() {
            @Override
            public void onSure(String string) {
                if (string != null && string.equals("1")) {
                    //微信分享
                    ShareService shareService = new ShareService();
                    shareService.share(activity, shareUrl, shareTitle, shareContent, imgUrl,
                            new ShareService.ShareListener() {
                                @Override
                                public void shareSuccess(SHARE_MEDIA share_media1) {

                                }

                                @Override
                                public void shareCancel() {

                                }
                            }, SHARE_MEDIA.WEIXIN);
                } else if (string != null && string.equals("2")) {
                    //朋友圈分享
                    ShareService shareService = new ShareService();
                    shareService.share(activity, shareUrl, shareTitle, shareContent, imgUrl,
                            new ShareService.ShareListener() {
                                @Override
                                public void shareSuccess(SHARE_MEDIA share_media1) {

                                }

                                @Override
                                public void shareCancel() {
                                    ToastUtils.custom("已取消分享");
                                }
                            }, SHARE_MEDIA.WEIXIN_CIRCLE);
                } else if (string != null && string.equals("3")) {
                    //复制链接
                    Toast.makeText(activity, "复制成功", Toast.LENGTH_SHORT).show();
                    ClipboardManager mClipboardManager =
                            (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData mClipData = ClipData.newPlainText("Label", shareUrl);
                    assert mClipboardManager != null;
                    mClipboardManager.setPrimaryClip(mClipData);
                }
            }
        });
        dialogShare.show();
    }

    /**
     * 获取图片文件
     *
     * @param activity
     * @return
     */
    public String getImageFile(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PermissionManager.checkFilePermission(activity)) {
                return FileUtils.getExternalPublicImageDir(activity);
            } else {
                return LKUtil.getAppConfig().getExternalImageCacheDir().getAbsolutePath();
            }
        } else {
            return FileUtils.getExternalPublicImageDir(activity);
        }
    }

}
